﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

namespace ZeroBug.EasyFlow.Core
{
    public class PathHelper
    {
        public static string GetConfigPath(string relativePath)
        {
            //string basedir = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\";
            //string basedir = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
            //string basedir = System.Environment.CurrentDirectory;
            string basedir = System.AppDomain.CurrentDomain.BaseDirectory;
            //string appname = Assembly.GetExecutingAssembly().GetName().Name + "\\";
            string appname = "";
            //string basedir = "D:\\";
            //string appname = "NextMachine\\";
            CreateConfigDirectory(basedir, appname, relativePath);
            return (basedir + appname + relativePath + "\\");
        }

        public static void CreateConfigDirectory(string baseDir, string appName, string path)
        {
            if(path.Contains("\\"))
            {
                CreateConfigDirectory(baseDir, appName, path.Split("\\")[0]);
            }
            else
            {
                if (!Directory.Exists(baseDir + appName + path))
                    Directory.CreateDirectory(baseDir + appName + path);
            }
        }

        public static bool IsFileExist(string fileName)
        {
            return System.IO.File.Exists(fileName);
        }

        public static void DeleteFile(string fileName)
        {
            System.IO.File.Delete(fileName);
        }

        public static void ListDir(string dir, out List<string> folderNames, out List<string> folderPaths)
        {
            folderNames = new List<string>();
            folderPaths = new List<string>();
            DirectoryInfo root = new DirectoryInfo(dir);
            DirectoryInfo[] dics = root.GetDirectories();
            foreach (DirectoryInfo di in dics)
            {
                folderNames.Add(di.Name);
                folderPaths.Add(di.FullName);
            }
        }

        public static List<FileInfo> ListFiles(string dir, params string[] extName)
        {
            List<FileInfo> fileList = new List<FileInfo>();
            if(!Directory.Exists(dir))
                return null;
            DirectoryInfo fdir = new DirectoryInfo(dir);
            FileInfo[] file = fdir.GetFiles();
            if (file.Length != 0 || dir.Length != 0) //当前目录文件或文件夹不为空                   
            {
                foreach (string ext in extName)
                {
                    foreach (FileInfo f in file) //显示当前目录所有文件   
                    {
                        if (ext.ToLower().IndexOf(f.Extension.ToLower()) >= 0)
                        {
                            fileList.Add(f);
                        }
                    }
                }
            }
            return fileList;
        }

        public static void DeleteDir(string dirPath)
        {
            try
            {
                //去除文件夹和子文件的只读属性
                //去除文件夹的只读属性
                System.IO.DirectoryInfo fileInfo = new DirectoryInfo(dirPath);
                fileInfo.Attributes = FileAttributes.Normal & FileAttributes.Directory;

                //去除文件的只读属性
                System.IO.File.SetAttributes(dirPath, System.IO.FileAttributes.Normal);

                //判断文件夹是否还存在
                if (Directory.Exists(dirPath))
                {
                    foreach (string f in Directory.GetFileSystemEntries(dirPath))
                    {
                        if (System.IO.File.Exists(f))
                        {
                            //如果有子文件删除文件
                            System.IO.File.Delete(f);
                            //Console.WriteLine(f);
                        }
                        else
                        {
                            //循环递归删除子文件夹
                            DeleteDir(f);
                        }
                    }
                    //删除空文件夹
                    Directory.Delete(dirPath);
                }

            }
            catch (Exception ex) // 异常处理
            {
                throw (ex);
            }
        }

        public static void CopyDirectory(string srcPath, string desPath, string desFolderName = "")
        {
            string folderName = "";
            folderName = desFolderName;

            string desfolderdir = desPath + "\\" + folderName;

            if (desPath.LastIndexOf("\\") == (desPath.Length - 1))
            {
                desfolderdir = desPath + folderName;
            }
            string[] filenames = Directory.GetFileSystemEntries(srcPath);

            foreach (string file in filenames)
            {
                if (Directory.Exists(file))
                {

                    string currentdir = desfolderdir + file.Substring(file.LastIndexOf("\\") + 1);
                    if (!Directory.Exists(currentdir))
                    {
                        Directory.CreateDirectory(currentdir);
                    }

                    CopyDirectory(file, desfolderdir + file.Substring(file.LastIndexOf("\\") + 1));
                }

                else
                {
                    string srcfileName = file.Substring(file.LastIndexOf("\\") + 1);

                    srcfileName = desfolderdir + "\\" + srcfileName;


                    if (!Directory.Exists(desfolderdir))
                    {
                        Directory.CreateDirectory(desfolderdir);
                    }


                    File.Copy(file, srcfileName);
                }
            }
        }
    }
}
